package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Contact;
import cn.wolfcode.crm.mapper.ContactMapper;
import cn.wolfcode.crm.query.ContactQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IContactService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContactServiceImpl implements IContactService {
    @Autowired
    private ContactMapper contactMapper;
    @Override
    public void deleteByPrimaryKey(Long id) {
        contactMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Contact entity) {
        contactMapper.insert(entity);
    }

    @Override
    public Contact selectByPrimaryKey(Long id) {

        return contactMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Contact> selectAll() {
        return contactMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Contact entity) {
        contactMapper.updateByPrimaryKey(entity);
    }


    @Override
    public PageResult query(QueryObject qo) {
        Integer total = contactMapper.query4Count(qo);
        if(total==0){
            return PageResult.PAGERESULT_EMPTY;
        }
        return new PageResult(total,contactMapper.query4List(qo));
    }

    /**
     * 根据条件查询员工列表导出到exl
     *
     * @param qo
     * @return
     */
    @Override
    public Workbook exportXls(ContactQueryObject qo) {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("性别");
        row.createCell(3).setCellValue("生日");
        row.createCell(4).setCellValue("所属学校");
        row.createCell(5).setCellValue("部门");
        row.createCell(6).setCellValue("职务");
        row.createCell(7).setCellValue("电话");
        row.createCell(8).setCellValue("QQ");
        row.createCell(9).setCellValue("邮箱");
        row.createCell(10).setCellValue("简介");

        List<?> list = contactMapper.query4List(qo);

        for (int i = 0; i < list.size(); i++) {
            try {
                Contact contact = (Contact) list.get(i);
                //创建行
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(contact.getId());
                row.createCell(1).setCellValue(contact.getName());
                row.createCell(2).setCellValue(contact.getGander());
                row.createCell(3).setCellValue(contact.getBirthday());
                row.createCell(4).setCellValue(contact.getUniversity() != null ? contact.getUniversity().getName() : "");
                row.createCell(5).setCellValue(contact.getDept());
                row.createCell(6).setCellValue(contact.getDuty());
                row.createCell(7).setCellValue(contact.getTel() );
                row.createCell(8).setCellValue(contact.getQq());
                row.createCell(9).setCellValue(contact.getEmail());
                row.createCell(10).setCellValue(contact.getIntro());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return book;
    }
}
